package org.exez.modules.visitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.exez.common.exception.JeecgBootException;
import org.exez.common.util.DateUtils;
import org.exez.modules.visitor.dto.terminal.DeviceTerminalDTO;
import org.exez.modules.visitor.entity.Device;
import org.exez.modules.visitor.entity.Institution;
import org.exez.modules.visitor.mapper.DeviceMapper;
import org.exez.modules.visitor.service.IDeviceService;
import org.exez.modules.visitor.service.IInstitutionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 终端设备
 * @Author: exez-boot
 * @Date:   2020-06-20
 * @Version: V1.0
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Autowired
    private IInstitutionService institutionService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDevice(DeviceTerminalDTO deviceTerminalDTO) {

        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDeviceCode, deviceTerminalDTO.getDeviceCode());
        queryWrapper.eq(Device::getDeleted, 1);
        Device selectOne = baseMapper.selectOne(queryWrapper);
        if (null != selectOne){
            if (!selectOne.getInstitutionCode().equals(deviceTerminalDTO.getInstitutionCode())) {
                throw new JeecgBootException("该设备已经关联了单位，请勿重复操作");
            }else {
                Device update = new Device();
                BeanUtils.copyProperties(deviceTerminalDTO,update);
                update.setId(selectOne.getId());
                baseMapper.updateById(update);
                return;
            }

        }

        Institution institutionByCode = institutionService.getInstitutionByCode(deviceTerminalDTO.getInstitutionCode());

        Date date = DateUtils.getDate();
        if (null == institutionByCode){

            LambdaQueryWrapper<Institution> institutionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            institutionLambdaQueryWrapper.eq(Institution :: getInstitutionName,deviceTerminalDTO.getInstitutionName());
            institutionLambdaQueryWrapper.eq(Institution :: getDeleted,1);
            int count = institutionService.count(institutionLambdaQueryWrapper);
            if (count > 0 ){
                throw new JeecgBootException("已存在该单位名称:" + deviceTerminalDTO.getInstitutionName());
            }

            institutionByCode = new Institution();
            institutionByCode.setInstitutionName(deviceTerminalDTO.getInstitutionName());
            institutionByCode.setCode(deviceTerminalDTO.getInstitutionCode());
            institutionByCode.setCreateTime(date);
            institutionByCode.setUpdateTime(date);
            institutionByCode.setDeleted(1);
            institutionService.save(institutionByCode);
        }

        Device insert = new Device();
        BeanUtils.copyProperties(deviceTerminalDTO,insert);
        insert.setCreateTime(date);
        insert.setUpdateTime(date);
        insert.setInstitutionId(institutionByCode.getId());
        baseMapper.insert(insert);

    }


    @Override
    public List<Device> listDeviceByInstitutionId(Integer institutionId) {
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getInstitutionId, institutionId);
        queryWrapper.eq(Device::getDeleted, 1);

        List<Device> devices = baseMapper.selectList(queryWrapper);

        return devices;
    }

    @Override
    public Device getDeviceByCodeAndInstitutionId(String code, Integer institutionId) {

        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Device::getDeviceCode,code);
        queryWrapper.eq(Device::getInstitutionId,institutionId);
        return baseMapper.selectOne(queryWrapper);
    }
}
